|
|
|
|
|
|
|
To alleviate this problem, you can assign the table an alias by using the AS portion of the FROM clause. Using AS, you can assign a unique, shorter name to each table. This alias can be used in all the other clauses in which the table name is needed. Listing C.5 is a rewrite of the code from Listing C.4, using the alias CS for the Customers table and ZP for the Zipcode table. |
|
|
|
|
|
|
|
|
Listing C.5 ALIAS.TXT Using a Table Alias to Cut Down on Typing |
|
|
|
|
|
|
|
|
'*****************************************************
'We use aliases to make the statement easier to enter.
'*****************************************************
SELECT CS.Lastname, CS.Firstname, ZP.City, ZP.State _
FROM Customers AS CS, Zipcode IN USZIPS AS ZP _
WHERE CS.Zip = ZP.Zip |
|
|
|
|
|
|
|
|
Using ALL, DISTINCT, or DISTINCTROW Predicates |
|
|
|
|
|
|
|
|
In most applications, you select all records that meet specified criteria. You can do this by specifying the ALL predicate in front of your field names or by leaving out any predicate specification (ALL is the default behavior). Therefore, the following two statements are equivalent: |
|
|
|
|
|
|
|
|
SELECT * FROM Customers
SELECT ALL * FROM Customers |
|
|
|
|
|
|
|
|
There might be times, however, when you want to determine the unique values of fields. For these times, use the DISTINCT or DISTINCTROW predicate. The DISTINCT predicate causes the database engine to retrieve only one record with a specific set of field valuesno matter how many duplicates exist. For a record to be rejected by the DISTINCT predicate, its values for all the selected fields must match those of another record. For example, if you are selecting first and last names, you can retrieve several people with the last name Smith, but you can't retrieve multiple occurrences of Adam Smith. |
|
|
|
|
|
|
|
|
If you want to eliminate records that are completely duplicated, use the DISTINCTROW predicate. DISTINCTROW compares the values of all fields in the table, whether or not they are among the selected fields. For the sample database, you can use DISTINCTROW to determine which products have been ordered at least once. DISTINCTROW has no effect if the query is on only a single table. |
|
|
|
|
|
|
|
|
Listing C.6 shows the uses of DISTINCT and DISTINCTROW. |
|
|
|
|
|
|
|
|
Listing C.6 DISTINCT.TXTObtaining Unique Records with the DISTINCT or DISTINCTROW Predicates |
|
|
|
|
|
|
|
|
'*****************************
'Use of the DISTINCT predicate
'*****************************
SELECT DISTINCT [Item Code] FROM Sales |
|
|
|
|
|